var Timer={
	setTimer:function(callbacker,param,interval){
		var newId=this.getNewId();
		this.timers[newId]=
			{
				callbacker:callbacker,
				param:param
			};

		setTimeout("Timer.handleTimeout("+newId+")",interval);
	},
	timers:[],
	getNewId:function(){
		var i=0;
		for(;i<this.timers.length;i++)
		{
			if(this.timers[i]==null)return i;
		}
		return i;
	},
	handleTimeout:function(id){
		var callbacker=this.timers[id].callbacker;
		var param=this.timers[id].param;
		this.timers[id]=null;
		callbacker.onTimeout(param);
		return;
	}
};

var Shell={
	exec:function(cmd){
		var WshShell= new ActiveXObject("WScript.Shell");
		return WshShell.exec(cmd);
	},
	run:function(cmd){
		var WshShell= new ActiveXObject("WScript.Shell");
		return WshShell.Run(cmd,0,true);
	}
}
var File={
	objFSO:new ActiveXObject("Scripting.FileSystemObject"),
	readAll:function(fn)
	{
		var oTextStream = this.objFSO.OpenTextFile(fn,1);
		var sFileContents = oTextStream.ReadAll();
		oTextStream.Close();
		return sFileContents;
	},
	getWindowsTemp:function()
	{
		return this.objFSO.GetSpecialFolder(2);
	},
	getTempName:function()
	{
		return this.objFSO.GetTempName();
	},
	getFullTempName:function()
	{
		return this.getWindowsTemp()+'\\'+this.getTempName();
	},
	convertUrlPathToLocalFilePath:function(url){
		return url.replace('file:///','').replace(/\//g,'\\');
	}

}

function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = new obj.constructor(); // changed (twice)
    for(var key in obj)
        temp[key] = clone(obj[key]);

    return temp;
}

var AsyncProcessHelper={
	exec:function(cmd,callbacker,param,checkInterval){
		var objFSO = File.objFSO ;
		
		var errFileName=File.getFullTempName();
		var outFileName=File.getFullTempName();
		var agentScriptFilePath=Environment.getScriptFolderPath()+'/AsyncProcessAgent.js';
		
		fullCmd='wscript //E:javascript "'+agentScriptFilePath+'" '+
				'"'+outFileName+'" "'+errFileName+'" "'+escape(cmd)+'"';
		//alert(fullCmd);
		var processState={
			obj:Shell.exec(fullCmd),
			callbacker:callbacker,
			outFileName:outFileName,
			errFileName:errFileName,
			checkInterval:checkInterval
		};
		Timer.setTimer(this,processState,checkInterval);
	},
	onTimeout:function(processState){
		if(processState.obj.Status==1)
		{
			
			var err=null;
			var out=null;
			//alert(processState.outFileName);
			try{
				out=File.readAll(processState.outFileName);
			}catch(e){}			
			try{
				err=File.readAll(processState.errFileName);
			}catch(e){}

			if(processState.callbacker!=null)
				processState.callbacker.onFinish({
					out:out,
					err:err,
					exitCode:processState.obj.ExitCode
				});
		}
		else
		{
			Timer.setTimer(this,processState,processState.checkInterval);
		}
	}
	
};

function getDumpObjString(e,indent)
{
	var ret=[];
	for(var x in e){
		var line="";
		for(var i=0;i<indent;i++){
			line+='\t';
		}
		line+=x+':'+e[x];
		ret.push(line);
		switch(typeof(e[x])){
		case 'object':
			ret.concat(getDumpObjString(e[x],indent+1));
		}
	}
	return ret;
}